﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class menu_uc_menu : DAO.ESWebUserControl
{
    public string contactAttr;
    public string filename = "";
    public bool profile = false;
    public bool logged = false;
    public bool isAdmin1 = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (this.Parent.Page.ToString().Contains("contactadmin"))
        {
            contactAttr = "background-color:#000000;color:#ffffff";
            ESi18nHyperLink20.Style.Add("color", "#ffffff");
        }
        if (Request.Url.Segments.Length >= 2)
        {
            if (Request.Url.Segments[1] != null && Request.Url.Segments[1] != "")
                filename = Request.Url.Segments[1];
        }
        int cid = 0;
        if (Request.QueryString["cid"] != null)
            Int32.TryParse(Request.QueryString["cid"].ToString(), out cid);

        if (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true")
        {
            profile = DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString()) == cid.ToString();
            ESi18nHyperLink18.NavigateUrl = "~/CompanyDetails.aspx?cid=" + DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString());
            //ESi18nHyperLink2.NavigateUrl = "~/CompanyDetails.aspx?cid=" + DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString());
        }
        logged = (Request.Cookies["texchange"] != null && Request.Cookies["texchange"]["logged"] != null && DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["logged"]) == "true");
        isAdmin1 = (profile &&
            DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["type"]) != "admin" ||
            filename.ToLower() == "myapplications.aspx" ||
                filename.ToLower() == "favorites_app.aspx");
        if (!Page.IsPostBack)
        {
            
        }
        
        //Response.Write(filename);
        LoadAppLinks();
        LoadCompLinks();
    }

    private void LoadCompLinks()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 2, " + this.LangId + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (this.LangId == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        HyperLink hl = new HyperLink();
                        hl.Text = dr["name"].ToString();
                        hl.NavigateUrl = "~/Company.aspx?catid=" + dr["id"].ToString();
                        phCompLinks.Controls.Add(hl);
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        HyperLink hl = new HyperLink();
                        hl.Text = dr["name"].ToString();
                        hl.NavigateUrl = "~/Company.aspx?catid=" + dr["parent_id"].ToString();
                        phCompLinks.Controls.Add(hl);
                    }
                }
            }
        }
    }

    private void LoadAppLinks()
    {
        Dictionary<string, string> offers = LoadOffers();
        Dictionary<string, string> app_types = LoadAppTypes();

        foreach (var offer in offers.Reverse())
        {
            if (offer.Key == "71")//Teders
            {
                HyperLink hl = new HyperLink();
                hl.Text = offer.Value;
                hl.NavigateUrl = "~/Application.aspx?offerid=" + offer.Key;
                phAppLinks.Controls.Add(hl);
            }
            else
            {
                foreach (var app in app_types)
                {
                    HyperLink hl = new HyperLink();
                    hl.Text = app.Value + " " + offer.Value;
                    hl.NavigateUrl = "~/Application.aspx?typeid=" + app.Key + "&offerid=" + offer.Key;
                    phAppLinks.Controls.Add(hl);
                }
            }
        }
    }

    private Dictionary<string, string> LoadAppTypes()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 9, " + this.LangId + ") WHERE isActive='true'", conn))
            {
                Dictionary<string, string> app_types = new Dictionary<string, string>();

                SqlDataReader dr = cmd.ExecuteReader();
                if (this.LangId == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        app_types[dr["id"].ToString()] = dr["name"].ToString();
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        app_types[dr["parent_id"].ToString()] = dr["name"].ToString();
                    }
                }
                dr.Close();
                return app_types;
            }
        }
    }

    private Dictionary<string, string> LoadOffers()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 12, " + this.LangId + ") WHERE isActive='true'", conn))
            {
                Dictionary<string, string> offers = new Dictionary<string, string>(); 
                SqlDataReader dr = cmd.ExecuteReader();
                if (this.LangId == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        offers[dr["id"].ToString()]  =  dr["name"].ToString();            
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        offers[dr["parent_id"].ToString()] = dr["name"].ToString(); 
                    }
                }
                dr.Close();
                return offers;
            }
        }
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
}
